﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

// Directive for the data model.
//using MyDeeWeibo.Settings;


namespace MyDeeWeibo
{
    public class DeeViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private DeeDataContext accountDB;

        // Class constructor, create the data context object.
        public DeeViewModel(string accountDBConnectionString)
        {
            accountDB = new DeeDataContext(accountDBConnectionString);
        }

        // All logined accounts.
        private ObservableCollection<SavedAccount> _allAccounts;
        public ObservableCollection<SavedAccount> AllAccounts
        {
            get { return _allAccounts; }
            set
            {
                _allAccounts = value;
                NotifyPropertyChanged("AllAccounts");
            }
        }

#if sample
        // To-do items associated with the home category.
        private ObservableCollection<SavedAccount> _homeToDoItems;
        public ObservableCollection<SavedAccount> HomeToDoItems
        {
            get { return _homeToDoItems; }
            set
            {
                _homeToDoItems = value;
                NotifyPropertyChanged("HomeToDoItems");
            }
        }

        // To-do items associated with the work category.
        private ObservableCollection<ToDoItem> _workToDoItems;
        public ObservableCollection<ToDoItem> WorkToDoItems
        {
            get { return _workToDoItems; }
            set
            {
                _workToDoItems = value;
                NotifyPropertyChanged("WorkToDoItems");
            }
        }

        // To-do items associated with the hobbies category.
        private ObservableCollection<ToDoItem> _hobbiesToDoItems;
        public ObservableCollection<ToDoItem> HobbiesToDoItems
        {
            get { return _hobbiesToDoItems; }
            set
            {
                _hobbiesToDoItems = value;
                NotifyPropertyChanged("HobbiesToDoItems");
            }
        }
#endif

        // A list of all accounts, used by the add account page.
        private List<SavedAccount> _accountsList;
        public List<SavedAccount> AccountsList
        {
            get { return _accountsList; }
            set
            {
                _accountsList = value;
                NotifyPropertyChanged("AccountsList");
            }
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            accountDB.SubmitChanges();
        }

        // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            // Specify the query for all accounts in the database.
            var savedAccountsInDB = from SavedAccount account in accountDB.Accounts.OrderByDescending(x => x.LastLogin)
                                select account;

            // Query the database and load all accounts.
            AllAccounts = new ObservableCollection<SavedAccount>(savedAccountsInDB);

#if sample
            // Specify the query for all categories in the database.
            var toDoCategoriesInDB = from ToDoCategory category in accountDB.Categories
                                     select category;


            // Query the database and load all associated items to their respective collections.
            foreach (ToDoCategory category in toDoCategoriesInDB)
            {
                switch (category.Name)
                {
                    case "Home":
                        HomeToDoItems = new ObservableCollection<ToDoItem>(category.ToDos);
                        break;
                    case "Work":
                        WorkToDoItems = new ObservableCollection<ToDoItem>(category.ToDos);
                        break;
                    case "Hobbies":
                        HobbiesToDoItems = new ObservableCollection<ToDoItem>(category.ToDos);
                        break;
                    default:
                        break;
                }
            }
#endif

            // Load a list of all accounts.
            AccountsList = accountDB.Accounts.ToList();

        }

        // Add an account to the database and collections.
        public void AddNewAccount(SavedAccount newAccount)
        {
            // Add an account to the data context.
            accountDB.Accounts.InsertOnSubmit(newAccount);

            // Save changes to the database.
            accountDB.SubmitChanges();

            // Add an account to the "all" observable collection.
            AllAccounts.Insert(0, newAccount);

#if sample
            // Add a to-do item to the appropriate filtered collection.
            switch (newToDoItem.Category.Name)
            {
                case "Home":
                    HomeToDoItems.Add(newToDoItem);
                    break;
                case "Work":
                    WorkToDoItems.Add(newToDoItem);
                    break;
                case "Hobbies":
                    HobbiesToDoItems.Add(newToDoItem);
                    break;
                default:
                    break;
            }
#endif
        }

        // Remove an account from the database and collections.
        public void DeleteAccount(SavedAccount accountForDelete)
        {

            // Remove the account from the "all" observable collection.
            AllAccounts.Remove(accountForDelete);

            // Remove the account item from the data context.
            accountDB.Accounts.DeleteOnSubmit(accountForDelete);

#if sample
            // Remove the to-do item from the appropriate category.   
            switch (toDoForDelete.Category.Name)
            {
                case "Home":
                    HomeToDoItems.Remove(toDoForDelete);
                    break;
                case "Work":
                    WorkToDoItems.Remove(toDoForDelete);
                    break;
                case "Hobbies":
                    HobbiesToDoItems.Remove(toDoForDelete);
                    break;
                default:
                    break;
            }
#endif

            // Save changes to the database.
            accountDB.SubmitChanges();
        }

        public void DelUserExisted(string testUID)
        {
            var savedIdsInDB = from SavedAccount account in accountDB.Accounts
                                    select account;
            var savedID = savedIdsInDB.ToArray();
            for (int i = 0; i < savedID.Length; i++)
            {
                if (testUID == savedID[i].UserId)
                {
                    DeleteAccount(savedID[i]);
                }
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
